package com.nuc.thread.countdownlatch;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.*;

/**
 * @author 诗酒趁年华
 * @version 1.0.0
 * @Description 等待多线程执行完毕
 * @createTime 2021年08月15日 16:46:00
 */
@Slf4j
public class TestCountDownLatch2 {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService pool = Executors.newFixedThreadPool(10);
        CountDownLatch latch = new CountDownLatch(10);
        String[] arr = new String[10];
        Random random = new Random();

        for (int j = 0; j < 10; j++) {
            int k = j;
            pool.submit(()->{
                for (int i = 0; i <= 100; i++) {
                    try {
                        arr[k] = i+"%";
                        Thread.sleep(random.nextInt(100));//线程随机睡眠100毫秒内的整数毫秒
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.print("\r"+ Arrays.toString(arr));
                }
                latch.countDown();
            });

        }

        latch.await();
        System.out.println("\n游戏开始!!!");
        pool.shutdown();

    }

}
